/********************************************
 * 功能说明: Web端OrganizationEmployeeRole基础业务控制器
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2019-12-18 18:41:08
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.controller;

import com.codahale.metrics.annotation.Timed;
import com.spring.arch.common.dto.result.ListResultDTO;
import com.spring.arch.common.dto.result.PageResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.web.converter.WebOrganizationEmployeeRoleConverter;
import com.spring.arch.uaa.web.dto.WebOrganizationEmployeeRoleDTO;
import com.spring.arch.common.dto.result.ListResultDTO;
import com.spring.arch.common.dto.result.PageResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.model.OrganizationEmployeeRole;
import com.spring.arch.uaa.repository.OrganizationEmployeeRoleRepository;
import com.spring.arch.uaa.service.OrganizationEmployeeRoleService;
import com.spring.arch.uaa.web.converter.WebOrganizationEmployeeRoleConverter;
import com.spring.arch.uaa.web.dto.WebOrganizationEmployeeRoleDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

import static com.spring.arch.uaa.errorcode.UaaErrorCode.E2001003001;


/**
 * Web端OrganizationEmployeeRole基础业务控制器
 *
 * @author
 * @version 1.0.0.1
 * @since 2019-12-18 18:41:08
 */
@Slf4j
@RestController
@RequestMapping(value = "/uaa/w/organizationEmployeeRole", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = {"Web端OrganizationEmployeeRole管理API"})
public class WebOrganizationEmployeeRoleController {

    @Autowired
    private OrganizationEmployeeRoleService organizationEmployeeRoleService;

    @Autowired
    private OrganizationEmployeeRoleRepository organizationEmployeeRoleRepository;

    @Autowired
    private WebOrganizationEmployeeRoleConverter organizationEmployeeRoleConverter;

    /**
     * 新建操作
     *
     * @param organizationEmployeeRoleDTOList 新建资源的DTO
     * @return 新建资源
     */
    @Timed
    @ApiOperation(value = "新建操作")
    @PostMapping
    public ResultDTO<Void> create(@RequestBody @Valid final List<WebOrganizationEmployeeRoleDTO> organizationEmployeeRoleDTOList) {
        final List<OrganizationEmployeeRole> model =
                this.organizationEmployeeRoleConverter.toListModel(organizationEmployeeRoleDTOList);
        this.organizationEmployeeRoleService.saveAll(model);
        if(log.isInfoEnabled()) {
            log.info("{} instance was created.", OrganizationEmployeeRole.class.getSimpleName());
        }
        return ResultDTO.success();
    }

    /**
     * 删除操作
     *
     * @param id 实体对象ID
     * @return 操作结果
     */
    @Timed
    @ApiOperation(value = "删除操作")
    @DeleteMapping(value = "/{id}")
    public ResultDTO<Void> delete(@PathVariable final UUID id) {
        this.organizationEmployeeRoleService.deleteById(id);
        if(log.isInfoEnabled()) {
            log.info("{} instance {} was disable.", OrganizationEmployeeRole.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }

    /**
     * 根据组织人员id删除全部的角色
     *
     * @param organizationEmployeeId 实体对象ID
     * @return 操作结果
     */
    @Timed
    @ApiOperation(value = "删除操作")
    @DeleteMapping(value = "/organizationEmployee/{organizationEmployeeId}")
    public ResultDTO<Void> deleteByOrganizationEmployee(@PathVariable final UUID organizationEmployeeId) {
        this.organizationEmployeeRoleService.deleteByOrganizationEmployee(organizationEmployeeId);
        if(log.isInfoEnabled()) {
            log.info("{} instance {} was disable.", OrganizationEmployeeRole.class.getSimpleName(),
                    organizationEmployeeId.toString());
        }
        return ResultDTO.success();
    }

    /**
     * 更新操作
     *
     * @param id                          实体对象ID
     * @param organizationEmployeeRoleDTO 更新资源的DTO
     * @return 更新后资源
     */
    @Timed
    @ApiOperation(value = "更新操作")
    @PutMapping(value = "/{id}")
    public ResultDTO<WebOrganizationEmployeeRoleDTO> update(@PathVariable final UUID id,
                                                            @RequestBody @Valid final WebOrganizationEmployeeRoleDTO organizationEmployeeRoleDTO) {
        organizationEmployeeRoleDTO.setId(id);
        final OrganizationEmployeeRole model =
                this.organizationEmployeeRoleConverter.toModel(organizationEmployeeRoleDTO);
        this.organizationEmployeeRoleService.save(model);
        if(log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", OrganizationEmployeeRole.class.getSimpleName(), model.getId());
        }
        return this.organizationEmployeeRoleConverter.toResultDTO(model);
    }

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @return 分页数据
     */
    @Timed
    @ApiOperation(value = "获取分页数据")
    @PostMapping(value = "/s")
    public PageResultDTO<WebOrganizationEmployeeRoleDTO> search(final Pageable pageable) {
        final Page<OrganizationEmployeeRole> models = this.organizationEmployeeRoleRepository.findAll(pageable);
        return this.organizationEmployeeRoleConverter.toResultDTO(models);
    }

    /**
     * 取得详细数据
     *
     * @param id 实体对象ID
     * @return 资源详细
     */
    @Timed
    @ApiOperation(value = "获取详细数据")
    @GetMapping(value = "/{id}")
    public ResultDTO<WebOrganizationEmployeeRoleDTO> get(@PathVariable final UUID id) {
        final OrganizationEmployeeRole model = this.organizationEmployeeRoleRepository.findById(id)
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001003001,
                        OrganizationEmployeeRole.class.getSimpleName()));
        return this.organizationEmployeeRoleConverter.toResultDTO(model);
    }

    /**
     * 根据组织人员id查询角色信息
     *
     * @param id 实体对象ID
     * @return 资源详细
     */
    @Timed
    @ApiOperation(value = "获取详细数据")
    @GetMapping(value = "/organizationEmployee/{id}")
    public ListResultDTO<WebOrganizationEmployeeRoleDTO> getByOrganizationEmployee(@PathVariable final UUID id) {
        final List<OrganizationEmployeeRole> model = this.organizationEmployeeRoleRepository.findByOrganizationEmployeeId(id);
        return this.organizationEmployeeRoleConverter.toResultDTO(model);
    }
}
